import http from "@/utils/http"
import store, { AppDispatch } from ".."


/**
 * 根据id获取文字详情
 */
export const getArticleDetails = (id: string) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get(`/v1_0/articles/${id}`)
    dispatch({ type: 'article/getInfo', payload: res.data })
  }
}


/**
 * 获取评论或评论回复
 */
type commentType = {
  type: 'a' | 'c'
  source: string
  offset?: string
}
export const getCommentList = ({ type, source, offset }: commentType) => {
  return async (dispatch: AppDispatch) => {
    const res = await http.get('/v1_0/comments', {
      params: {
        type,
        source,
        offset
      }
    })
    const { commentList } = store.getState().article

    let newList;
    if (commentList.results) {
      newList = { ...commentList, results: [...commentList.results, ...res.data.results] }

    } else { newList = res.data }

    dispatch({ type: 'article/setCommentList', payload: newList })
  }
}